package com.softeem.ssm2;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import sun.awt.AppContext;

import javax.servlet.Filter;

/**
 * webapp核心启动类，等同于web.xml
 * @Author mrchai
 * @Date 2019/10/12 16:39
 */
public class WepAppInit extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 获取applicationContext核心配置(spring核心配置)
     * @return
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{AppContextConfig.class};
    }

    /**
     * 获取springmvc核心配置
     * @return
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebMvcConfig.class};
    }

    /**
     * 配置核心控制器DispatcherServlet 拦截规则（url-pattern）
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     * 配置过滤器
     * @return
     */
    @Override
    protected Filter[] getServletFilters() {
        //创建编码过滤器
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("utf-8");
        encodingFilter.setForceResponseEncoding(true);
        return new Filter[]{encodingFilter};
    }
}
